package com.gongpingjialoan.activity.car;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.data.CarListFilterData;
import com.gongpingjialoan.global.GPJApplication;

/***
 * 首页-买车 hqh
 *
 * @author Administrator
 */
public class BuyCarFragment extends Fragment {

    View mainV;

    public CarListFragment carListFragment;

    public NewCarFragment newCarFragment;

    public NewRankFragment rankFragment;

    private Fragment currentFragment;

    FragmentManager fm;

    public Bundle saveFilterBundle;

    private final int REQUEST_FILTER = 0;

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mainV = inflater.inflate(R.layout.buy_car_fragment, null);
        fm = getActivity().getSupportFragmentManager();
        changeFragment();
        return mainV;
    }


    public void changeFragment() {
        if (MyPushIntentService.BILLBOARDTYPE.equals(MainActivity.main.fromType) || MyPushIntentService.BILLBOARD.equals(MainActivity.main.fromType) || MainActivity.USERHOME.equals(MainActivity.main.fromType)) {
            MainActivity.main.brandRadioButton.setChecked(true);
        } else {
            MainActivity.main.buycarRadioButton.setChecked(true);
        }
    }


    /**
     * 车型库
     */
    public void changCarListFragment() {
        if (carListFragment == null) {
            carListFragment = new CarListFragment();
        }
        if (getActivity() == null) return;
        if (fm == null) {
            fm = getActivity().getSupportFragmentManager();
        }
        FragmentTransaction t = fm.beginTransaction();

        if (rankFragment != null) {
            t.hide(rankFragment);
        }
        if (newCarFragment != null) {
            t.hide(newCarFragment);
        }
        if (fm.getFragments().contains(carListFragment)) {
            t.show(carListFragment);
        } else {
            t.add(R.id.content, carListFragment);
        }
        currentFragment = carListFragment;
        t.commitAllowingStateLoss();
    }


    /**
     * 榜单
     */
    public void changRankFragment() {
        if (rankFragment == null) {
            rankFragment = new NewRankFragment();
        }
        if (fm == null) {
            fm = getActivity().getSupportFragmentManager();
        }
        FragmentTransaction t = fm.beginTransaction();

        if (carListFragment != null) {
            t.hide(carListFragment);
        }
        if (newCarFragment != null) {
            t.hide(newCarFragment);
        }
        if (fm.getFragments().contains(rankFragment)) {
            t.show(rankFragment);
        } else {
            t.add(R.id.content, rankFragment);
        }
        currentFragment = rankFragment;
        t.commitAllowingStateLoss();
    }

    /**
     * 新车
     */
    public void changNewCarFragment() {
        if (newCarFragment == null) {
            newCarFragment = new NewCarFragment();
        }
        if (fm == null) {
            fm = getActivity().getSupportFragmentManager();
        }
        FragmentTransaction t = fm.beginTransaction();

        if (carListFragment != null) {
            t.hide(carListFragment);
        }
        if (rankFragment != null) {
            t.hide(rankFragment);
        }
        if (fm.getFragments().contains(newCarFragment)) {
            t.show(newCarFragment);
        } else {
            t.add(R.id.content, newCarFragment);
        }
        currentFragment = newCarFragment;
        t.commitAllowingStateLoss();
    }


    public void updateRankListData(String type) {
        if (rankFragment != null && rankFragment.isAdded()) {
            rankFragment.refresh(type);
        }
    }

    public void showFilterView() {
        Intent intent = new Intent();
        if (saveFilterBundle != null) {
            intent.putExtras(saveFilterBundle);
        }
        intent.setClass(getActivity(), CarListFilterActivity.class);
        startActivityForResult(intent, REQUEST_FILTER);
    }


    public void updateListData(String fromtype) {
        if (carListFragment != null && carListFragment.isAdded()) {
            //重新获取数据
            carListFragment.front_cover = 0;

            carListFragment.getSaveData(fromtype);
            carListFragment.addFilterView();
            if (!TextUtils.isEmpty(fromtype))
                carListFragment.fromType = fromtype;
            carListFragment.refresh();
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (REQUEST_FILTER == requestCode) {
                CarListFilterData.getInstance().setIsCleanData(false);
                updateListData(CarListFragment.FROM_FILTER);
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            if (REQUEST_FILTER == requestCode) {
                //not todo
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && carListFragment != null && GPJApplication.getInstance().isChangeCity) {
            showCityChange(MainActivity.main.currentCity);
        }

        //为了收到carListFragment ，rankFragment 的 onHiddenChanged事件
        if (hidden && currentFragment != null) {
            getActivity().getSupportFragmentManager().beginTransaction().hide(currentFragment).commitAllowingStateLoss();
        } else {
            getActivity().getSupportFragmentManager().beginTransaction().show(currentFragment).commitAllowingStateLoss();
        }
    }


    private void showCityChange(final String name) {

        CarListFilterData.getInstance().clear();
        CarListFilterData.getInstance().setCity(name);
        carListFragment.front_cover = 0;
        carListFragment.data.setIsCleanData(true);
        carListFragment.getSaveData("");
        carListFragment.fromType = "";
        carListFragment.refresh();

        GPJApplication.getInstance().isChangeCity = false;

    }

}